#!/bin/bash

HIVE_HOME=/usr/bin/hive
#上个月1日
Last_Month_DATE=$(date -d "$(date +%Y%m)01 last month" +%Y-%m-01)

${HIVE_HOME} -e "
--分区
SET hive.exec.dynamic.partition=true;
SET hive.exec.dynamic.partition.mode=nonstrict;
SET hive.exec.max.dynamic.partitions.pernode=10000;
SET hive.exec.max.dynamic.partitions=100000;
SET hive.exec.max.created.files=150000;
--hive压缩
SET hive.exec.compress.intermediate=true;
SET hive.exec.compress.output=true;
--写入时压缩生效
SET hive.exec.orc.compression.strategy=COMPRESSION;
--分桶
SET hive.enforce.bucketing=true;
SET hive.enforce.sorting=true;
SET hive.optimize.bucketmapjoin = true;
SET hive.auto.convert.sortmerge.join=true;
SET hive.auto.convert.sortmerge.join.noconditionaltask=true;
--并行执行
SET hive.exec.parallel=true;
SET hive.exec.parallel.thread.number=8;
--矢量化查询
SET hive.vectorized.execution.enabled=true;
--关联优化器
SET hive.optimize.correlation=true;
--读取零拷贝
SET hive.exec.orc.zerocopy=true;
--join数据倾斜
SET hive.optimize.skewjoin=true;
--set hive.skewjoin.key=100000;
SET hive.optimize.skewjoin.compiletime=true;
SET hive.optimize.union.remove=true;
--插入数据
INSERT INTO TABLE itcast_dwm.customer_signup_dwm PARTITION (payment_time_year, payment_time_month, payment_time_day)
SELECT dwd.customer_id,
       zz.itcast_school_id,
       zz.itcast_school_name,
       zz.itcast_subject_id,
       zz.itcast_subject_name,
       ee.tdepart_id,
       de.name AS tdepart_name,
       dwd.origin_type,
       dwd.origin_type_stat,
       dwd.payment_time_hour,
       dwd.payment_time_year,
       dwd.payment_time_month,
       dwd.payment_time_day
FROM itcast_dwd.customer_relationship_dwd dwd
LEFT JOIN itcast_dimen.itcast_clazz zz ON SUBSTR(dwd.payment_time, 1, 10) >= '${Last_Month_DATE}' AND dwd.itcast_clazz_id = zz.id
LEFT JOIN itcast_dimen.employee ee ON dwd.creator = ee.id
LEFT JOIN itcast_dimen.scrm_department de ON ee.tdepart_id = de.id
"